package cn.intcast.order.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.intcast.order.R;
import cn.intcast.order.actvity.FoodActivity;
import cn.intcast.order.bean.FoodBean;

public class MenuAdapter extends BaseAdapter {
    private Context mContext;
    private List<FoodBean> fbl;                   //菜单列表数据
    private OnSelectListener onSelectListener;

    public MenuAdapter(Context mContext, OnSelectListener onSelectListener) {
        this.mContext = mContext;
        this.onSelectListener=onSelectListener;
    }

    public void setData(List<FoodBean> fbl) {
        this.fbl = fbl;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return fbl == null ? 0 : fbl.size();
    }

    @Override
    public FoodBean getItem(int i) {
        return fbl == null ? null : fbl.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder vh;
        if (view == null) {
            vh = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.menu_item,
                    null);
            vh.tv_food_name = view.findViewById(R.id.tv_food_name);
            vh.tv_taste =  view.findViewById(R.id.tv_taste);
            vh.tv_sale_num = view.findViewById(R.id.tv_sale_num);
            vh.tv_price =  view.findViewById(R.id.tv_price);
            vh.btn_add_car = view.findViewById(R.id.btn_add_car);
            vh.iv_food_pic =  view.findViewById(R.id.iv_food_pic);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        final FoodBean bean = getItem(i);
        if (bean != null) {
            vh.tv_food_name.setText(bean.getFoodName());
            vh.tv_taste.setText(bean.getTaste ());
            vh.tv_sale_num.setText("月售" + bean.getSeleNum());
            vh.tv_price.setText("￥"+bean.getPrice());
            Glide.with(mContext)
                    .load(bean.getFoodPic())
                    .error(R.mipmap.ic_launcher)
                    .into(vh.iv_food_pic);
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean == null)return;
                Intent intent = new Intent(mContext, FoodActivity.class);
                intent.putExtra("food", bean);
                mContext.startActivity(intent);
            }
        });
        vh.btn_add_car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { //加入购物车按钮的点击事件
                onSelectListener.onSelectAddCar(i);
            }
        });
        return view;
    }
    class ViewHolder {
        public TextView tv_food_name, tv_taste, tv_sale_num, tv_price;
        public Button btn_add_car;
        public ImageView iv_food_pic;
    }
    public interface OnSelectListener {
        void onSelectAddCar(int position);
    }
}
